package com.sk.bilibili.view.diy;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.sk.bilibili.R;
import com.sk.bilibili.bean.Search;
import com.sk.bilibili.callBack.TagClickCallBack;
import com.sk.bilibili.databinding.ViewSearchTipBinding;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;

public class SearchTipView extends LinearLayout implements TagFlowLayout.OnTagClickListener {

    private ViewSearchTipBinding bind;
    private List<Search> tipList;
    private SearchTagAdapter tipAdapter;
    private List<Search> historyList;
    private SearchTagAdapter historyAdapter;
    private TagClickCallBack tagClickCallBack;

    public void setTagClickCallBack(TagClickCallBack tagClickCallBack) {
        this.tagClickCallBack = tagClickCallBack;
    }

    public void setClearListener(OnClickListener clearListener) {
        bind.tvClear.setOnClickListener(clearListener);
    }

    public SearchTipView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        bind = ViewSearchTipBinding.inflate(LayoutInflater.from(context), this, true);
        initView();
        bindEvent();
    }

    private void initView() {
        tipList=new ArrayList<>();
        tipAdapter=new SearchTagAdapter(tipList);
        bind.tflTip.setAdapter(tipAdapter);
        historyList=new ArrayList<>();
        historyAdapter=new SearchTagAdapter(historyList);
        bind.tflHistory.setAdapter(historyAdapter);
    }

    public void setTip(List<Search> list){
        tipAdapter.setData(list);
    }

    public void setHistory(List<Search> list){
        historyAdapter.setData(list);
    }

    public void clearHistory(){
        historyAdapter.clear();
    }

    private void bindEvent() {
        bind.tflTip.setOnTagClickListener(this);
        bind.tflHistory.setOnTagClickListener(this);
    }

    @Override
    public boolean onTagClick(View view, int position, FlowLayout parent) {
        if(tagClickCallBack==null)return false;
        if(parent==bind.tflTip){
            tagClickCallBack.onTagClick(tipList.get(position).getContent());
        }else {
            tagClickCallBack.onTagClick(historyList.get(position).getContent());
        }
        return true;
    }

    class SearchTagAdapter extends TagAdapter<Search>{

        private List<Search> list;

        public void setData(List<Search> list){
            this.list.clear();
            this.list.addAll(list);
            notifyDataChanged();
        }

        public void clear(){
            list.clear();
            notifyDataChanged();
        }

        public SearchTagAdapter(List<Search> datas) {
            super(datas);
            this.list=datas;
        }

        @Override
        public View getView(FlowLayout parent, int position, Search search) {
            TextView view = (TextView) LayoutInflater.from(getContext()).inflate(R.layout.search_tip_item, parent, false);
            view.setText(search.getContent());
            return view;
        }

    }

}